//
//  MovieList.swift
//  AppleDemo
//
//  Created by Just 花语 on 2025/10/28.
//


import SwiftUI
import SwiftData


struct MovieList: View {
    @Query private var movies: [Movie]
    @Environment(\.modelContext) private var context
    @State private var newMovie:Movie?
    
    init(titleFilter:String=""){
        let predicate = #Predicate<Movie>{ movie in
            titleFilter.isEmpty || movie.title.localizedStandardContains(titleFilter)
        }
        _movies = Query(filter: predicate,sort: \Movie.title)
    }
    
    var body: some View {
        Group{
            if !movies.isEmpty{
                List{
                    ForEach(movies){ movie in
                        NavigationLink(movie.title) {
                            MovieDetail(movie: movie)
                        }
                    }
                    .onDelete(perform: deleteMovies(indexes:))
                }
            }else{
                ContentUnavailableView("Add Movies", systemImage: "film.stack")
            }
        }
        .navigationTitle("Movies")
        .toolbar{
            ToolbarItem{
                Button("Add movie",systemImage: "plus",action: addMovie)
                    
            }
            ToolbarItem(placement: .navigationBarTrailing){
                EditButton()
            }
        }
        .sheet(item: $newMovie){ movie in
            NavigationStack {
                MovieDetail(movie: movie, isNew: true)
            }
        }
    }
    
    private func addMovie(){
        let movie = Movie(title: "", releaseDate: .now)
        context.insert(movie)
        self.newMovie = movie
    }
    private func deleteMovies(indexes: IndexSet) {
        for index in indexes {
            context.delete(movies[index])
        }
    }
}


#Preview {
    NavigationStack{
        MovieList()
            .modelContainer(SampleData.shared.modelContainer)
    }
}
